package com.ouli.crowd.mvc.handler;

import com.github.pagehelper.PageInfo;
import com.ouli.crowd.entity.Role;
import com.ouli.crowd.service.api.RoleService;
import com.ouli.crowd.util.ResultEntity;
import com.ouli.crowd.util.StringsOfInt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class RoleHandler {

    @Autowired
    private RoleService roleService;

    @ResponseBody
    @RequestMapping("/role/deleteByIds/info.json")
    public ResultEntity deleteByIds(@RequestParam("roleId")String ids) {
        int[] arr = StringsOfInt.valueOf(ids);
        roleService.deleteByIds(arr);
        return ResultEntity.successWithoutData();
    }
    @ResponseBody
    @RequestMapping("/role/updateById/info.json")
    public ResultEntity updateById(@RequestParam("id") Integer id,
                                   @RequestParam("roleName") String roleName) {
        roleService.updateById(new Role(id,roleName));
        return ResultEntity.successWithoutData();
    }
    @ResponseBody
    @RequestMapping("/role/selectByIds/info.json")
    public ResultEntity<List<Role>> selectByIds(@RequestParam("roleId") String roleId) {
        int[] ids =StringsOfInt.valueOf(roleId);

        return ResultEntity.successWithData(roleService.selectByIds(ids));
    }
    @ResponseBody
    @RequestMapping("/role/save/to/info.json")
    public ResultEntity saveRole(@RequestParam("roleName") String roleName) {
        roleService.saveRole(new Role(null,roleName));
        return ResultEntity.successWithoutData();
    }
    @ResponseBody
    @RequestMapping("/role/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(@RequestParam(value = "pageNum" ,defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize" ,defaultValue = "5") Integer pageSize,
                                                    @RequestParam(value = "keyword" ,defaultValue = "") String keyword){
        // 调用roleService方法获取分页数据
        PageInfo<Role> pageInfo = roleService.getPageInfo(keyword,pageNum,pageSize);

            return ResultEntity.successWithData(pageInfo);


    }
}
